<template>
  <el-form
    ref="deptForm"
    :model="deptForm"
    label-width="120px"
    :rules="deptRules"
  >
    <el-form-item label="部门名称" prop="name">
      <el-input
        v-model="deptForm.name"
        style="width: 80%"
        placeholder="2-10个字符"
      />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input
        v-model="deptForm.code"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门负责人">
      <el-select
        v-model="deptForm.manager"
        style="width: 80%"
        placeholder="请选择"
      >
        <el-option
          v-for="item in emloyess"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input
        v-model="deptForm.introduce"
        style="width: 80%"
        placeholder="1-300个字符"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        size="small"
        @click="subHandler"
      >确定</el-button>
      <el-button size="small" @click="cancelHandler">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeeSimple } from '@/api/emloyees'
import { addDepartments, updateDepartment } from '@/api/departments'
export default {
  name: 'Deptdialog',
  props: {
    pid: {
      type: String,
      default: ''
    },
    row: {
      required: true,
      type: [Object, Array]
    },
    isEdit: {
      required: true,
      type: Boolean
    }
  },
  data() {
    const checkName = (rule, value, callback) => {
      if (value.length < 2 || value.length > 10) {
        callback(new Error('部门名称不能小于2位或大于10位'))
      }
      callback()
    }
    return {
      deptForm: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      },
      deptRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }

        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' }
        ]
      },
      emloyess: []
    }
  },
  watch: {
    isEdit: {
      handler(newVal, oldVal) {
        // console.log(this.row, newVal)
        // 因为数据是传递过来的,直接使用表单双向数据绑定会影响源头数据所以需要使用深拷贝
        if (newVal) {
          this.deptForm = JSON.parse(JSON.stringify(this.row))
        }
      },
      // 初次进入立即执行
      immediate: true
    }
  },

  created() {
    this.loadEmployee()
    // console.log(this)
  },
  methods: {
    // 提交
    subHandler() {
    //   console.log(this.pid)
      this.deptForm.pid = this.pid
      this.$refs.deptForm.validate(async(result) => {
        if (result) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    // 添加
    async doAdd() {
      const res = await addDepartments(this.deptForm)
      console.log(res)
      if (res.code === 10000) {
        this.$refs.deptForm.resetFields()
      }
      this.$emit('closeDialog')
    },
    // 修改
    async doEdit() {
      const res = await updateDepartment(this.deptForm)
      console.log(res)
      if (res.code === 10000) {
        this.$refs.deptForm.resetFields()
      }
      this.$emit('closeDialog')
    },
    // 取消
    cancelHandler() {
      this.$refs.deptForm.resetFields()
      this.$emit('closeDialog')
    },
    async loadEmployee() {
      const res = await getEmployeeSimple()
      //   console.log(res.data)
      this.emloyess = res.data
    },
    // 清空数据
    close() {
      // 清空数据
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
